<template>
  <div>
    <div class="box">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="工单编号">
          <el-input v-model="formInline.taskCode" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="工单状态">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option label="代办" value="1" />
            <el-option label="进行" value="2" />
            <el-option label="取消" value="3" />
            <el-option label="完成" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="margin: 0px"
            @click="onSubmit"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-button type="warning" @click="add">+新建</el-button>
    </el-row>
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column label="序号" width="120">
        <template v-slot="{ $index }">
          <span>{{
            (formInline.pageIndex - 1) * formInline.pageSize + $index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="taskCode" label="工单编号" width="180" />
      <el-table-column prop="innerCode" label="设备编号" />
      <el-table-column prop="taskType.typeName" label="工单类型" />
      <el-table-column label="工单方式">
        <template v-slot="{ row }">
          <span>{{ row.createType === 0 ? "自动" : "手动" }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="taskStatusTypeEntity.statusName" label="工单状态">
        <template v-slot="{ row }">
          <span>{{
            row.taskStatus === 1
              ? "代办"
              : row.taskStatus === 2
                ? "进行"
                : row.taskStatus === 3
                  ? "取消"
                  : row.taskStatus === 4
                    ? "完成"
                    : "状态未知"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="运营人员" />
      <el-table-column prop="createTime" label="创建日期" />
      <el-table-column prop="address" label="操作">
        <template v-slot="{row}">
          <!-- <template> -->
          <el-button type="text" @click="detail(row.taskId)">查看详情</el-button>
          <!-- <el-button type="text" @click="detail">查看详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <el-col>
        <el-button type="primary" plain @click="beforepage">上一页</el-button>
        <el-button type="primary" plain @click="next">下一页</el-button>
      </el-col>
    </el-row>
    <!-- 新增工单 -->
    <el-dialog v-if="dialogFormVisible" title="新增工单" :visible.sync="dialogFormVisible" width="700px">
      <el-form
        ref="formList"
        :model="form"
        :rules="rules"
        style="width: 600px; height: 350px"
        label-width="80px"
        @close="handleClose"
      >
        <el-form-item label="设备编号" prop="innerCode">
          <el-input
            v-model="form.innerCode"
            autocomplete="off"
            placeholder="请输入"
            @input="changeFn"
          />
        </el-form-item>
        <el-form-item label="工单类型" prop="productType">
          <el-select
            v-model="form.productType"
            placeholder="请选择"
            style="width: 520px"
          >

            <el-option v-for="(item,index) in seach" :key="index" :label="item.typeName" :value="item.typeId" />

          </el-select>
        </el-form-item>

        <el-form-item label="运维人员" prop="assignorId">
          <el-select
            v-model="form.assignorId"
            placeholder="请选择"
            style="width: 520px"
          >
            <template>
              <el-option v-for="(item,index) in list" :key="index" :label="item.userName" :value="item.userId" />
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="desc">
          <el-input
            v-model="form.desc"
            autocomplete="off"
            type="textarea"
            :rows="3"
            placeholder="请输入备注(不超过40字)"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button
                type="warning"
                @click="addList"
              >确 定</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 工单详情 -->
    <el-dialog
      v-if="dialogVisible2"
      title="工单详情"
      :visible.sync="dialogVisible2"
      width="40%"
    >
      <div class="imgbgc">
        <div class="picleft">
          <img
            class="pic1"
            src=""
            alt=""
          >
          <span class="type">{{ result.taskStatusTypeEntity.statusName }}</span>
        </div>
        <img
          class="pic"
          src="https://likede2-admin.itheima.net/img/pic_1.834b274c.png"
          alt=""
        >
      </div>
      <div class="bigbox">
        <div class="box1">
          <span>设备编号：</span>
          <span>{{ result.innerCode }}</span>
        </div>
        <div class="box1">
          <span>创建日期：</span>
          <span>{{ result.createTime }}</span>
        </div>
        <div class="box1">
          <span>运维人员：</span>
          <span>{{ result.userName }}</span>
        </div>
        <div class="box1">
          <span>工单类型：</span>
          <span>{{ result.productTypeId===1? '投放工单': result.productTypeId===2? '补货工单':result.productTypeId===3? '维修工单':'撤机工单' }}</span>
        </div>
        <div class="box1">
          <span>工单方式：</span>
          <span>{{ result.createType===0 ? '自动':'手动' }}</span>
        </div>
        <div class="box1">
          <span>备注：</span>
          <span>{{ result.desc }}</span>
        </div>
      </div>
      <el-row type="flex" justify="center">
        <el-button type="warning" plain @click="dialogFormVisible = true">重新创建</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { devops, detail, person, getList, addnum } from '@/api/user'
export default {
  data() {
    return {
      list: [],
      seach: [],
      loading: false,
      form: {
        innerCode: '',
        productType: '',
        assignorId: '',
        desc: '',
        userId: 1,
        detail: [],
        createType: 1
      },

      dialogFormVisible: false,
      dialogVisible2: false,
      result: {},
      formInline: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
        taskCode: '',
        status: '',
        isRepair: true
      },
      userName: '',
      tableData: [],
      rules: {
        innerCode: [{ required: true, message: '设备编号不能为空', trigger: 'blur' }],
        productType: [
          { required: true, message: '工单类型不能为空', trigger: 'blur' }
        ],
        assignorId: [
          { required: true, message: '运维人员不能为空', trigger: 'blur' }
        ],
        desc: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const getdevops = await devops(this.formInline)
      // console.log(getdevops)
      this.total = getdevops.totalCount
      this.tableData = getdevops.currentPageRecords
    },
    handleClose() {
      this.$refs.formList.resetFields()
      this.dialogVisible2 = false
    },
    add() {
      this.dialogFormVisible = true
    },
    async changeFn() {
      const res = await person(this.form.innerCode)
      this.list = res
      const res2 = await getList()
      this.seach = res2
      // res2.forEach((item) => { this.seach.push(item.userId) })
      // res2.forEach((item) => { this.seach.push(item.userName) })
      // console.log(this.seach)

      // console.log(res2)
    },
    async getdata() {
      this.loading = true
      const res = await devops({ ...this.formInline })
      // console.log(res)
      this.tableData = res.currentPageRecords
      this.loading = false
    },
    async onSubmit() {
      this.getdata()
    },

    beforepage() {
      if (this.formInline.pageIndex !== 1) {
        this.formInline.pageIndex--
        this.getdata()
      } else {
        this.$message.warning('已经是第一页')
      }
    },
    next() {
      this.formInline.pageIndex++
      this.getdata()
    },
    async detail(taskId) {
      this.dialogVisible2 = true
      const res = await detail(taskId)
      this.result = res
      console.log(this.result)
      // console.log(res)
    },
    addList() {
      this.$refs.formList.validate(async pass => {
        if (pass) {
          const res = await addnum({ ...this.form })
          console.log(res)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  height: 60px;
}
.el-button {
  margin: 20px;
}
.el-col {
  text-align: right;
}
.imgbgc {
  width: 100%;
  align-items: center;
  height: 54px;
  margin-bottom: 25px;
  background-color: hsla(0, 0%, 92.5%, 0.39);
  display: flex;
  justify-content: space-between;
  .picleft {
    .pic1{
      margin-left: 22px;
       margin-top: 20px;
       margin-right: 10px;
    }
    .type{
      display: inline-block;
      height:50px;
      line-height: 48px;
    }
  }
  .pic {
    margin-right: 76px;
    margin-bottom: 7px;
  }
}
.bigbox{
  display: flex;
  width: 100%;
  margin-left: 30px;
    flex-wrap: wrap;
    margin-bottom: 10px;
  .box1{
    width: 50%;
    margin-top: 20px;

  }
}
</style>
